package com.itcast.SessionUtils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SessionUtils {
    private SessionUtils() {}

    private static SqlSessionFactory build =null;
    static {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sql.xml");
            build = new SqlSessionFactoryBuilder().build(resourceAsStream);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                resourceAsStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    public static SqlSession getSession(){
        return build.openSession();
    }
}
